package com.example.mall.controller;


import com.alibaba.fastjson.JSON;
import com.example.mall.pojo.*;
import com.example.mall.service.CategoryService;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@AllArgsConstructor
@CrossOrigin
public class CategoriesController {

    CategoryService categoryService;

    @DeleteMapping("/categories/{id}")
    public Rbody updateCategoryId(@PathVariable Integer id){
        Rbody rbody = new Rbody();
        System.out.println(id);
        Boolean deleteCategory = categoryService.deleteCategory(id);
        if(!deleteCategory){
            rbody.getMeta().setStatus(400);
            rbody.getMeta().setMsg("删除类别信息失败");
        }else {
            rbody.getMeta().setStatus(200);
            rbody.getMeta().setMsg("删除类别信息成功");
        }

        return rbody;
    }



    @PutMapping("/categories/{id}")
    public Rbody updateCategoryId(@PathVariable Integer id,@RequestBody String  body){
        Rbody rbody = new Rbody();
        System.out.println(id);
        Category category1 = JSON.parseObject(body, Category.class);

        Category categoryName = categoryService.getCategoryName(category1.getCat_name());
        if(categoryName==null){
            Boolean category = categoryService.updateCategory(id, category1.getCat_name());
            System.out.println(category);
            if(!category){
                rbody.getMeta().setStatus(400);
                rbody.getMeta().setMsg("更新类别信息失败");
            }else {
                Category categoryById = categoryService.getCategoryById(id);
                System.out.println(categoryById);
                rbody.setData(categoryById);
                rbody.getMeta().setStatus(200);
                rbody.getMeta().setMsg("更新类别信息成功");
            }
        }else {
            rbody.getMeta().setStatus(400);
            rbody.getMeta().setMsg("更新类别信息失败");
        }



        return rbody;
    }



    @GetMapping("/categories/{id}")
    public Rbody getCategoryId(@PathVariable Integer id){
        Rbody rbody = new Rbody();
        System.out.println(id);
        Category category = categoryService.getCategoryById(id);
        if(category==null){
            rbody.getMeta().setStatus(400);
            rbody.getMeta().setMsg("获取类别信息失败");
        }else {
            rbody.setData(category);
            rbody.getMeta().setStatus(200);
            rbody.getMeta().setMsg("获取类别信息成功");
        }

        return rbody;
    }


    @PostMapping("/categories")
    public Rbody getCategory(@RequestBody String body){
        Rbody rbody = new Rbody();
        Category category = JSON.parseObject(body, Category.class);
        Integer level = category.getCat_level();
        String catName = category.getCat_name();
        Integer pid = category.getCat_pid();

        Category categoryName = categoryService.getCategoryName(catName);
        if(categoryName==null){
            Boolean addCategory = categoryService.addCategory(pid, catName, level);
            if(!addCategory){
                rbody.getMeta().setStatus(400);
                rbody.getMeta().setMsg("增加类别失败");
            }else {
                rbody.getMeta().setStatus(201);
                rbody.getMeta().setMsg("创建类别成功");
            }
        }else {
            rbody.getMeta().setStatus(400);
            rbody.getMeta().setMsg("增加类别失败");
        }



        return rbody;
    }



    @GetMapping("/categories")
    public Rbody getCategory( Integer type, Integer pagenum, Integer pagesize){


        Rbody rbody = new Rbody();
        NewCategoryList newCategoryList = new NewCategoryList();
       List<Level1> list = new ArrayList<>();
        ArrayList<Level1> level2ArrayList = new ArrayList<>();
        Level1 newLevel = new Level1();
        if(type==3){
            if(pagenum!=null || pagesize!=null){
                List<Level1> categoryLevelFirst1 = categoryService.getCategoryLevelFirst1(pagenum,pagesize);
                for (Level1 level11: categoryLevelFirst1) {
//                System.out.println(level11);
                    Integer cat_id = level11.getCat_id();
                    List<Level2> categoryLevelSecond = categoryService.getCategoryLevelSecond(cat_id);
                    level11.setChildren(categoryLevelSecond);
//                System.out.println(level11);
                    for (Level2 level21: categoryLevelSecond) {
//                    System.out.println("level21"+level21);
                        Integer cat_id1 = level21.getCat_id();
                        List<Level3> categoryLevelThird1 = categoryService.getCategoryLevelThird(cat_id1);
                        level21.setChildren(categoryLevelThird1);
//                    System.out.println("++++++++++++"+level21);

                    }
                    list.add(level11);
                }
                newCategoryList.setResult(list);
                PageInfo<Level1> pageAll = categoryService.getPageAll2(pagenum,pagesize);
                long total = pageAll.getTotal();
                newCategoryList.setPagenum(pagenum);
                System.out.println(pagenum);
                System.out.println(pagesize);
                newCategoryList.setPagesize(pagesize);
                newCategoryList.setTotal((int) total);
                rbody.setData(newCategoryList);
                rbody.getMeta().setMsg("获取分类成功");
                rbody.getMeta().setStatus(200);
            }else {
                List<Level1> categoryLevelFirst = categoryService.getCategoryLevelFirst();

                for (Level1 level11: categoryLevelFirst) {
                    System.out.println(level11);
                    Integer cat_id = level11.getCat_id();
                    List<Level2> categoryLevelSecond = categoryService.getCategoryLevelSecond(cat_id);
                    level11.setChildren(categoryLevelSecond);
                    for (Level2 level21: categoryLevelSecond) {
                        Integer cat_id1 = level21.getCat_id();
                        List<Level3> categoryLevelThird1 = categoryService.getCategoryLevelThird(cat_id1);
                        level21.setChildren(categoryLevelThird1);

                    }
                    list.add(level11);

                    level2ArrayList.add(level11);
                }

                rbody.setData(level2ArrayList);
                rbody.getMeta().setMsg("显示分类");
                rbody.getMeta().setStatus(200);
            }


        }else {

            List<Level1> categoryLevelFirst = categoryService.getCategoryLevelFirst();

            for (Level1 level11: categoryLevelFirst) {
                System.out.println(level11);
                Integer cat_id = level11.getCat_id();
                List<Level2> categoryLevelSecond = categoryService.getCategoryLevelSecond(cat_id);
                level11.setChildren(categoryLevelSecond);
                System.out.println(level11);
                level2ArrayList.add(level11);
            }

            rbody.setData(level2ArrayList);
            rbody.getMeta().setMsg("显示分类");
            rbody.getMeta().setStatus(200);
        }




        return rbody;
    }





}
